﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    using DAL.Entities;

    public class ThuocDAL : BaseDAL, UTL.IBaseDAL 
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            string sql = @"SELECT *,b.donvitinh,c.tenloaithuoc from THUOC a join DOnVITINH b on a.madvt=b.madvt join LOAITHUOC c on a.maloaithuoc=c.maloaithuoc";

            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @"DELETE FROM thuoc WHERE mathuoc= '{0}'";
            sql = string.Format(sql,id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (thuoc)obj;
            string sql = @"INSERT INTO thuoc(mathuoc,tenthuoc,madvt,maloaithuoc) VALUES('{0}',N'{1}','{2}','{3}')";
            sql = string.Format(sql,o.mathuoc,o.tenthuoc,o.madvt,o.maloaithuoc);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (thuoc)obj;
            string sql = @" UPDATE thuoc SET tenthuoc=N'{0}', madvt='{1}',maloaithuoc='{2}' WHERE mathuoc='{3}' ";
            sql = string.Format(sql, o.tenthuoc, o.madvt, o.maloaithuoc,o.mathuoc);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        public DataTable Select_madvt()
        {
            string sql = @"SELECT madvt as id, donvitinh as ten FROM DONVITINH";
            return ExecuteQuery(sql);
        }

        public DataTable Select_maloaithuoc()
        {
            string sql = @" SELECT maloaithuoc as id, tenloaithuoc as ten FROM LOAITHUOC";
            return ExecuteQuery(sql);
        }
        /// <summary>
        /// lay ...
        /// </summary>
        /// <returns></returns>
        public string Select_max()
        {
            string sql = @"SELECT max(substring(MATHUOC,2,LEN(MATHUOC)-1)) as id FROM thuoc";
            var tb= ExecuteQuery(sql);
            
            return tb.Rows.Count>0? tb.Rows[0]["id"]+"":"0";
        }



        public bool deleteobj(object obj)
        {
            throw new NotImplementedException();
        }
    }
}
